

// hi Su hui, there are 3 methods in this class you can copy to your Player class
// there are two attributes that need to be decalred within your class

// they are:
//		String filename="userInfo.txt";
//		bufferedWriter = null;

// here is the explanation of the 3 methods i wrote

		//1.
// createFile()     
//: if a textfile "userInfo.txt" does not already exist, then create such a new file. 
//else do nothing. 
//MUST be called once when program starts

		//2.
// saveToFile(String name, String password, String level, String experience)    
//: self-explanatory , but note that new info will be added to the end of the current file, 
//it's addedon instead of overwrite

		//3.
// exportToVector()
//: export info in current textfile into temporary strings such as String username, 
// i think you should make these strings public attributes within your Play class 
// such that you can do something like player1.Username= username
// do you understand , it sounds a bit confusing i guess  =P


// Lastly, reminder to su hui:
// when a new user registers his new username, 
// please remeber to check if this name has been taken up already by others
// End of Read me


import java.io.*;
import java.util.*;





public class save {
	
	String filename="userInfo.txt";
	BufferedWriter bufferedWriter = null;
	
	public void createFile()
	{
		File file=new File(filename);
		if(!file.exists())  // if this file does not exist yet, create a new file
		{
			try 
			{
				bufferedWriter = new BufferedWriter(new FileWriter(filename));
			} catch (FileNotFoundException ex) 
			{ex.printStackTrace();} catch (IOException ex)
			{ex.printStackTrace();} 
			
			finally
			{
	            //Close the BufferedWriter
	            try 
				{
	                if (bufferedWriter != null) 
					{
	                    bufferedWriter.flush();
	                    bufferedWriter.close();
	                }
	            } catch (IOException ex) 
				{ ex.printStackTrace();}		
			}
			
		}//end of if(!file.exists())
		
	}  // end of createfile()
	
	public void saveToFile(String name, String password, String level, String experience)
	{
		try 
		{ 	
			
			boolean append = true; 
			FileWriter fw = new FileWriter(filename,append); 
			
			fw.write(name);
			fw.write(" ");
			fw.write(password);
			fw.write(" ");
			fw.write(level);
			fw.write(" ");
			fw.write(experience);
			fw.write("\n");
			fw.close(); 
		} 
		
		catch(IOException ioe) 
		{ 
			System.err.println("IOException: " + ioe.getMessage()); 
		} 	
		
	}//end of saveToFile()
	
	
	public void exportToVector(){
		
		try{
			
			File f = new File(filename);
			FileInputStream fstream = new FileInputStream(f);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			
			//Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				
				StringTokenizer st	= new StringTokenizer(strLine);
				
				
				//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! here are where the strings are saved
				String username		= st.nextToken();
				String password		= st.nextToken();
				String level		= st.nextToken();
				String experience	= st.nextToken();
				
				
				///this part is only for demostration purpose only, can delete
				System.out.println( "					For User ----  "		+username);
				System.out.println("Username:		"		+ username);
				System.out.println("Passowrd:		"		+ password);
				System.out.println("Level:			"		+ level);  
				System.out.println("Experience:   "		+ experience);
				System.out.println( "\n");  
				// can delete till here
				
				
				
				
				
				
			}
			
			//Close the input stream
			in.close();
			
		}catch (Exception e){
			//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
		
		
	} //end of exportToVector()
	
	
    public static void main (String args[]) 
	{
		
		String username="Bilal";
		String password="BilalPassword";
		new save().createFile();
		
		new save().saveToFile(username,password,"0","10");
		new save().saveToFile(username, password, "34", "20");
		
		new save().exportToVector();
	}
	
}